// 引入当前页面的less文件 
require("../less/index.less")
// 引入字体图标
require("../fonts/iconfont.css");
//引入轮播图
// require("../css/swiper-bundle.min.css");
// var Swiper=require("../lib/swiper-bundle.min.js");
document.ready(function () {
  var mySwiper = new Swiper('.swiper', {
    direction: 'horizontal', // 垂直切换选项
    loop: true, // 循环模式选项
    autoplay: {
      delay: 3000,
      stopOnLastSlide: false,
      disableOnInteraction: false,
    },
    // 如果需要分页器
    pagination: {
      el: '.swiper-pagination',
    },
  })
  //获取
  let rankNum = document.querySelector("#rankNum");
  let sumadd = document.querySelector("#sumadd");
  let sumNum = document.querySelector("#sumNum");
  let badgeNum = document.querySelector("#badgeNum");
  //获取本地存储中的用户信息
  let strUser = window.sessionStorage.getItem("user")
  let userObj = JSON.parse(strUser);
  //  今日排名  
  //设置请求头部的token
  axios.defaults.headers.authorization = `Bearer ${userObj.token}`;
  // 先查看是否登录 
  axios.get(window.$util.portNum + "/api/user/info")
    .then(res => {
      //  console.log(res);
      //  未登录
      if (res.data.errno === 10003) {
        window.$util.popupBox("jieshu", res.data.message);
        window.setTimeout(() => {
          window.location.href = "login.html"
        }, 2001)
      } else if (res.data.errno === 0) {
        //  已登录
        //排名
        rankNum.innerHTML = res.data.data.ranking;
        //徽章数
        badgeNum.innerHTML = res.data.data.badges;
        //累计打卡天数
        sumadd.innerHTML = res.data.data["member.clockCount"]
      }
    })
    .catch(error => {
      console.log(error);
    })
  //打卡
  let flag = true;
  sumNum.addEventListener("click", () => {
    if (flag) {
      axios.post(window.$util.portNum + "/api/user/clockIn")
        .then(res => {
          // console.log(res);
          //打卡成功
          if (res.data.errno === 0) {
            window.$util.popupBox("gougou", "打卡成功");
            sumNum.innerHTML = "已打卡";
            flag = !flag;
            //请求打卡次数
            axios.get(window.$util.portNum + "/api/user/clockInInfo")
              .then(res => {
                console.log(res);
                if (res.data.errno === 0) {
                  sumadd.innerHTML = res.data.data.clockCount;
                }
              }).catch(error => {
                console.log(error);
              })
          } else if (res.data.errno === -1) {
            //打卡失败
            window.$util.popupBox("jieshu", "今日已打卡");
            sumNum.innerHTML = "已打卡"

            //请求打卡次数
            axios.get(window.$util.portNum + "/api/user/clockInInfo")
              .then(res => {
                console.log(res);
                if (res.data.errno === 0) {
                  sumadd.innerHTML = res.data.data.clockCount;
                }
              }).catch(error => {
                console.log(error);
              })
          }

        }).catch(error => {
          console.log(error);
        })
    } else {
      sumNum.innerHTML = "已打卡"
      window.$util.popupBox("jieshu", "今日已打卡");
    }
  })
})
